#!/usr/bin/env sh

Check_Authorized()
{
  Echo_Blue "配置公钥..."

  if [ -f /root/.ssh/authorized_keys ]; then
    if ! cat /root/.ssh/authorized_keys | grep -Eqi ${SSH_KEY}; then
      \cp /root/.ssh/authorized_keys /root/.ssh/authorized_keys_$(date +"%Y%m%d%H%M%S")
      echo ${SSH_RSA} >> /root/.ssh/authorized_keys
      echo "已配置 SSH 公钥: ${SSH_KEY}"
    fi
  else 
    mkdir /root/.ssh
    cat > /root/.ssh/authorized_keys<<EOF
${SSH_RSA}
EOF
  fi
}

Set_Timezone()
{
  Echo_Blue "设置时区..."

  rm -rf /etc/localtime
  ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  date

  # install.conf
  sed -i -e "s|TIME_ZONE=.*|TIME_ZONE=${TIME_ZONE}|g;" ${INSTALL_CONF}
  # docker-compose.yml
  sed -i -e "s|TIME_ZONE|${TIME_ZONE}|g;" ${DOCKER_COMPOSE_YML}
}

Debian_Modify_Source()
{
  SourceListUrl="https://mirrors.aliyun.com/debian/"
  # CodeName=$(lsb_release -cs)

  DebSecurity="-security"
  DebSecurityNote=''
  DebUpdates="-updates"
  DebBackportsNote=''

  if echo "${DISTRO_Version}" | grep -Eqi '^7'; then
    SourceListUrl="https://mirrors.aliyun.com/debian-archive/debian/"
    CodeName='wheezy'
    DebSecurityNote="#"
    DebUpdates="-proposed-updates"
    DebBackportsNote="#"
  elif echo "${DISTRO_Version}" | grep -Eqi '^8'; then
    CodeName='jessie'
    DebSecurityNote="#"
    DebUpdates="-proposed-updates"
    DebBackportsNote="#"
  elif echo "${DISTRO_Version}" | grep -Eqi '^9'; then
    CodeName='stretch'
    DebSecurity="/updates"
  elif echo "${DISTRO_Version}" | grep -Eqi '^10'; then
    CodeName='buster'
    DebSecurity="/updates"
  elif echo "${DISTRO_Version}" | grep -Eqi '^11'; then
    CodeName='bullseye'
  fi

  echo "SourceUrl: ${SourceListUrl}"
  
  if [ "${CodeName}" != "" ]; then
    \cp /etc/apt/sources.list /etc/apt/sources.list.$(date +"%Y%m%d%H%M%S")
    cat > /etc/apt/sources.list<<EOF
deb ${SourceListUrl} ${CodeName} main non-free contrib
deb-src ${SourceListUrl} ${CodeName} main non-free contrib
${DebSecurityNote}deb https://mirrors.aliyun.com/debian-security ${CodeName}${DebSecurity} main
${DebSecurityNote}deb-src https://mirrors.aliyun.com/debian-security ${CodeName}${DebSecurity} main
deb ${SourceListUrl} ${CodeName}${DebUpdates} main non-free contrib
deb-src ${SourceListUrl} ${CodeName}${DebUpdates} main non-free contrib
${DebBackportsNote}deb ${SourceListUrl} ${CodeName}-backports main non-free contrib
${DebBackportsNote}deb-src ${SourceListUrl} ${CodeName}-backports main non-free contrib
EOF
  fi
}

RHEL_Modify_Source()
{
  Get_RHEL_Version
  if [ "${RHELRepo}" = "local" ]; then
    echo "DO NOT change RHEL repository, use the repository you set."
  else
    echo "RHEL ${RHEL_Ver} will use aliyun centos repository..."
    if [ ! -s "/etc/yum.repos.d/Centos-${RHEL_Ver}.repo" ]; then
      wget --prefer-family=IPv4 http://mirrors.aliyun.com/repo/Centos-${RHEL_Ver}.repo -O /etc/yum.repos.d/Centos-${RHEL_Ver}.repo
    fi
    if echo "${RHEL_Version}" | grep -Eqi "^6"; then
      sed -i "s#centos/\$releasever#centos-vault/\$releasever#g" /etc/yum.repos.d/Centos-${RHEL_Ver}.repo
      sed -i "s/\$releasever/${RHEL_Version}/g" /etc/yum.repos.d/Centos-${RHEL_Ver}.repo
    else
      sed -i "s/\$releasever/${RHEL_Ver}/g" /etc/yum.repos.d/Centos-${RHEL_Ver}.repo
    fi
    yum clean all
    yum makecache
  fi
  sed -i "s/^enabled[ ]*=[ ]*1/enabled=0/" /etc/yum/pluginconf.d/subscription-manager.conf
}

Ubuntu_Modify_Source()
{
  SourceListUrl="https://mirrors.aliyun.com/ubuntu/"
  CodeName=$(lsb_release -cs)

  echo "SourceUrl: ${SourceListUrl}"

  if [ "${CodeName}" != "" ]; then
    \cp /etc/apt/sources.list /etc/apt/sources.list.$(date +"%Y%m%d%H%M%S")
    cat > /etc/apt/sources.list<<EOF
deb ${SourceListUrl} ${CodeName} main restricted universe multiverse
deb ${SourceListUrl} ${CodeName}-security main restricted universe multiverse
deb ${SourceListUrl} ${CodeName}-updates main restricted universe multiverse
deb ${SourceListUrl} ${CodeName}-proposed main restricted universe multiverse
deb ${SourceListUrl} ${CodeName}-backports main restricted universe multiverse
deb-src ${SourceListUrl} ${CodeName} main restricted universe multiverse
deb-src ${SourceListUrl} ${CodeName}-security main restricted universe multiverse
deb-src ${SourceListUrl} ${CodeName}-updates main restricted universe multiverse
deb-src ${SourceListUrl} ${CodeName}-proposed main restricted universe multiverse
deb-src ${SourceListUrl} ${CodeName}-backports main restricted universe multiverse
EOF
  fi
}

CentOS6_Modify_Source()
{
  if echo "${CentOS_Version}" | grep -Eqi "^6"; then
    Echo_Yellow "CentOS 6 is now end of life, use vault repository."
    \cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    \cp ${CurrentDir}/conf/CentOS6-Base-Vault.repo /etc/yum.repos.d/CentOS-Base.repo
  fi
}

Update_Dist()
{
  Echo_Blue "更新系统..."

  # 更新源
  # vi /etc/resolv.conf
  # nameserver 223.5.5.5
  # nameserver 8.8.8.8
  # nameserver 8.8.4.4
  # /etc/init.d/networking restart

  if [ "$PM" = "yum" ]; then
    yum clean all
    yum update
  elif [ "$PM" = "apt" ]; then
    apt-get clean
    apt-get update
    apt-get upgrade
  fi
}

# 更新服务器配置
Update_Server_Conf()
{
  Echo_Blue "更新服务器配置..."

  # SERVER_IP
  if [ -z ${SERVER_IP} ]; then
    read -p "服务器 IP 地址(默认 127.0.0.1): " SERVER_IP
  else
    echo "SERVER_IP=${SERVER_IP}"
  fi
  if [ -z ${SERVER_IP} ]; then
    SERVER_IP=127.0.0.1
  fi

  # NETWORK_UUID
  if [ -z ${NETWORK_UUID} ]; then
    read -p "专网标识(默认为服务器 IP 地址): " NETWORK_UUID
  else
    echo "NETWORK_UUID=${NETWORK_UUID}"
  fi
  if [ -z ${NETWORK_UUID} ]; then
    NETWORK_UUID=${SERVER_IP}
  fi

  # SSH_USER
  if [ -z ${SSH_USER} ]; then
    read -p "服务器 SSH 账号(默认 root): " SSH_USER
  else
    echo "SSH_USER=${SSH_USER}"
  fi
  if [ -z ${SSH_USER} ]; then
    SSH_USER=root
  fi

  # SSH_PORT
  if [ -z ${SSH_PORT} ]; then
    read -p "服务器 SSH 端口(默认 22): " SSH_PORT
  else
    echo "SSH_PORT=${SSH_PORT}"
  fi
  if [ -z ${SSH_PORT} ]; then
    SSH_PORT=22
  fi

  # install.conf
  sed -i -e "s|SERVER_IP=.*|SERVER_IP=${SERVER_IP}|g;" ${INSTALL_CONF}
  sed -i -e "s|NETWORK_UUID=.*|NETWORK_UUID=${NETWORK_UUID}|g;" ${INSTALL_CONF}
  sed -i -e "s|SSH_USER=.*|SSH_USER=${SSH_USER}|g;" ${INSTALL_CONF}
  sed -i -e "s|SSH_PORT=.*|SSH_PORT=${SSH_PORT}|g;" ${INSTALL_CONF}
  # sed -i -e "s|THEME_VERSION=.*|THEME_VERSION=${THEME_VERSION}|g;" ${INSTALL_CONF}
}

# 安装 curl
Install_Curl()
{
  echo "[+] Installing curl..."
  if [ "$PM" = "yum" ]; then
    yum -y install curl
  elif [ "$PM" = "apt" ]; then
    apt-get --no-install-recommends install -y curl
  fi
}

# 检查端口是否被占用
Check_Port()
{
  ERROR=
  if lsof -i:${1} | grep -Eqi 'LISTEN'; then
    ERROR="${1} 端口已被占用！"
    Echo_Red "${ERROR}"
    lsof -i:${1}
  fi
}

# 退出确认
Exit_Confirm()
{
  confirm="n"
  read -p "是否退出？(y/N)" confirm
  if [ "${confirm}" == "y" ]; then
    exit 1;
  fi
}

